package com.heifeng.cloudshelf.mvp.web.presenter;

import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.mvp.web.contract.WebViewActivityContract;

import javax.inject.Inject;

/**
 * Created by Administrator on 2019/9/18.
 */

public class WebViewActivityPresenter extends RxPresenter<WebViewActivityContract.View> implements WebViewActivityContract.Presenter {

    private WebChromeClient webChromeClient = new WebChromeClient() {
        public boolean onConsoleMessage(ConsoleMessage param1ConsoleMessage) {
            return super.onConsoleMessage(param1ConsoleMessage);
        }

        public void onProgressChanged(WebView param1WebView, int param1Int) {
        }
    };

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return true;
        }

    };

    @Inject
    public WebViewActivityPresenter() {
    }

    @Override
    public void initWebView() {
        WebSettings settings = mView.getWebView().getSettings();
        settings.setJavaScriptEnabled(true);

        settings.setAllowFileAccess(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(true);
        // 设置可以支持缩放
        settings.setSupportZoom(false);
        //扩大比例的缩放
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setDefaultTextEncodingName("utf-8");

        settings.setDomStorageEnabled(true);//设置适应HTML5的一些方法
        settings.setAppCachePath(mContext.getCacheDir().getAbsolutePath());
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);

        settings.supportMultipleWindows();
        settings.setSupportMultipleWindows(true);

        mView.getWebView().setVerticalScrollBarEnabled(false);
        mView.getWebView().setHorizontalScrollBarEnabled(false);
        mView.getWebView().setWebViewClient(webViewClient);
        mView.getWebView().setWebChromeClient(webChromeClient);
    }

    @Override
    public void loadUrl(String url) {
        mView.getWebView().loadUrl(url);
    }

    @Override
    public void destoryWebView() {
        if (null != mView.getWebView()) {
            mView.getWebView().clearHistory();
            mView.getWebView().clearCache(true);
            mView.getWebView().destroy();
        }
    }

}
